// pages/videocontentdeail/videocontentdeail.js
import { find } from "../../api/article"
import { find as productfind } from "../../api/product"
import {
  buyNow
} from '../../api/cart'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    articlelist: {},
    productId: "",
    productList: {},
    article_content: ''
  },
  //立即购买的按键事件
  buySubmit() {
    var _this = this
    this.checkLogin(() => {
      let data = {}
      data.productId =  this.data.productId
      data.quantity = 1
      buyNow(data).then(
        res => {
          if (res.type == 'success') {
            if(_this.data.articlelist.liveId){
              wx.navigateTo({
                url: '/pages/goods/submit/index?liveId='+ _this.data.articlelist.liveId,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }else{
              wx.navigateTo({
                url: '/pages/goods/submit/index',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
           
          }
        }
      ).catch(error => {
        console.log(error)
        wx.showToast({
          title: error.content,
          icon: 'none',
          duration: 2000
        })
      })
    })
  },
  //检测用户的登录状态
  checkLogin(callback) {
    console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      if (callback) {
        callback()
      }
    }
  },
  navTo() {
    let p = getCurrentPages().length;
    if (p <= 1) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  findclick() {
    find(this.data.id).then(res => {
      console.log(res);
      this.data.articlelist = res.data.article
      this.setData({
        articlelist: this.data.articlelist,
        article_content: this.data.article_content
      })
    })
  },
  findproduct() {
    productfind(this.data.productId).then(res => {
      this.data.productList = res.data.products[0]
      this.setData({
        productList: this.data.productList
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNavSize()
    console.log("85655965652645641534153153215313");
    console.log(options);
    this.data.id = options.id
    this.data.productId = options.productId
    this.findclick()
    this.findproduct()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})